home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / glibc108.zip / glibc108 / inet / res_query.c < prev    next >
C/C++ Source or Header  |  1993-05-19  |  10KB  |  305 lines

  1. /*
  2.  * ++Copyright++ 1988
  3.  * -
  4.  * Copyright (c) 1988 Regents of the University of California.
  5.  * All rights reserved.
  6.  * 
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  * 1. Redistributions of source code must retain the above copyright
  11.  *    notice, this list of conditions and the following disclaimer.
  12.  * 2. Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  * 3. All advertising materials mentioning features or use of this software
  16.  *    must display the following acknowledgement:
  17.  *     This product includes software developed by the University of
  18.  *     California, Berkeley and its contributors.
  19.  * 4. Neither the name of the University nor the names of its contributors
  20.  *    may be used to endorse or promote products derived from this software
  21.  *    without specific prior written permission.
  22.  * 
  23.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  24.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  27.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  29.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  30.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  33.  * SUCH DAMAGE.
  34.  * -
  35.  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
  36.  * 
  37.  * Permission to use, copy, modify, and distribute this software for any
  38.  * purpose with or without fee is hereby granted, provided that the above
  39.  * copyright notice and this permission notice appear in all copies, and that
  40.  * the name of Digital Equipment Corporation not be used in advertising or
  41.  * publicity pertaining to distribution of the document or software without
  42.  * specific, written prior permission.
  43.  * 
  44.  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
  45.  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  46.  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
  47.  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  48.  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  49.  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
  50.  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  51.  * SOFTWARE.
  52.  * -
  53.  * --Copyright--
  54.  */
  55.  
  56. #if defined(LIBC_SCCS) && !defined(lint)
  57. static char sccsid[] = "@(#)res_query.c    5.11 (Berkeley) 3/6/91";
  58. static char rcsid[] = "$Id: res_query.c,v 4.9.1.1 1993/05/02 22:43:03 vixie Rel $";
  59. #endif /* LIBC_SCCS and not lint */
  60.  
  61. #include <sys/param.h>
  62. #include <netinet/in.h>
  63. #include <arpa/inet.h>
  64. #include <arpa/nameser.h>
  65. #include <netdb.h>
  66. #include <resolv.h>
  67. #include <stdio.h>
  68. #include <ctype.h>
  69. #include <errno.h>
  70. #include "../conf/portability.h"
  71.  
  72. #if PACKETSZ > 1024
  73. #define MAXPACKET    PACKETSZ
  74. #else
  75. #define MAXPACKET    1024
  76. #endif
  77.  
  78. int h_errno;
  79.  
  80. /*
  81.  * Formulate a normal query, send, and await answer.
  82.  * Returned answer is placed in supplied buffer "answer".
  83.  * Perform preliminary check of answer, returning success only
  84.  * if no error is indicated and the answer count is nonzero.
  85.  * Return the size of the response on success, -1 on error.
  86.  * Error number is left in h_errno.
  87.  * Caller must parse answer and determine whether it answers the question.
  88.  */
  89. res_query(name, class, type, answer, anslen)
  90.     char *name;        /* domain name */
  91.     int class, type;    /* class and type of query */
  92.     u_char *answer;        /* buffer to put answer */
  93.     int anslen;        /* size of answer buffer */
  94. {
  95.     char buf[MAXPACKET];
  96.     HEADER *hp;
  97.     int n;
  98.  
  99.     if ((_res.options & RES_INIT) == 0 && res_init() == -1)
  100.         return (-1);
  101. #ifdef DEBUG
  102.     if (_res.options & RES_DEBUG)
  103.         printf(";; res_query(%s, %d, %d)\n", name, class, type);
  104. #endif
  105.     n = res_mkquery(QUERY, name, class, type, (char *)NULL, 0, NULL,
  106.         buf, sizeof(buf));
  107.  
  108.     if (n <= 0) {
  109. #ifdef DEBUG
  110.         if (_res.options & RES_DEBUG)
  111.             printf(";; res_query: mkquery failed\n");
  112. #endif
  113.         h_errno = NO_RECOVERY;
  114.         return (n);
  115.     }
  116.     n = res_send(buf, n, (char *)answer, anslen);
  117.     if (n < 0) {
  118. #ifdef DEBUG
  119.         if (_res.options & RES_DEBUG)
  120.             printf(";; res_query: send error\n");
  121. #endif
  122.         h_errno = TRY_AGAIN;
  123.         return(n);
  124.     }
  125.  
  126.     hp = (HEADER *) answer;
  127.     if (hp->rcode != NOERROR || ntohs(hp->ancount) == 0) {
  128. #ifdef DEBUG
  129.         if (_res.options & RES_DEBUG)
  130.             printf(";; rcode = %d, ancount=%d\n", hp->rcode,
  131.                 ntohs(hp->ancount));
  132. #endif
  133.         switch (hp->rcode) {
  134.             case NXDOMAIN:
  135.                 h_errno = HOST_NOT_FOUND;
  136.                 break;
  137.             case SERVFAIL:
  138.                 h_errno = TRY_AGAIN;
  139.                 break;
  140.             case NOERROR:
  141.                 h_errno = NO_DATA;
  142.                 break;
  143.             case FORMERR:
  144.             case NOTIMP:
  145.             case REFUSED:
  146.             default:
  147.                 h_errno = NO_RECOVERY;
  148.                 break;
  149.         }
  150.         return (-1);
  151.     }
  152.     return(n);
  153. }
  154.  
  155. /*
  156.  * Formulate a normal query, send, and retrieve answer in supplied buffer.
  157.  * Return the size of the response on success, -1 on error.
  158.  * If enabled, implement search rules until answer or unrecoverable failure
  159.  * is detected.  Error number is left in h_errno.
  160.  * Only useful for queries in the same name hierarchy as the local host
  161.  * (not, for example, for host address-to-name lookups in domain in-addr.arpa).
  162.  */
  163. int
  164. res_search(name, class, type, answer, anslen)
  165.     char *name;        /* domain name */
  166.     int class, type;    /* class and type of query */
  167.     u_char *answer;        /* buffer to put answer */
  168.     int anslen;        /* size of answer */
  169. {
  170.     register char *cp, **domain;
  171.     int n, ret, got_nodata = 0;
  172.     char *__hostalias();
  173.  
  174.     if ((_res.options & RES_INIT) == 0 && res_init() == -1)
  175.         return (-1);
  176.  
  177.     errno = 0;
  178.     h_errno = HOST_NOT_FOUND;    /* default, if we never query */
  179.     for (cp = name, n = 0;  *cp;  cp++)
  180.         if (*cp == '.')
  181.             n++;
  182.     if (n == 0 && (cp = __hostalias(name)))
  183.         return (res_query(cp, class, type, answer, anslen));
  184.  
  185.     /*
  186.      * We do at least one level of search if
  187.      *    - there is no dot and RES_DEFNAME is set, or
  188.      *    - there is at least one dot, there is no trailing dot,
  189.      *      and RES_DNSRCH is set.
  190.      */
  191.     if ((n == 0 && _res.options & RES_DEFNAMES) ||
  192.        (n != 0 && *--cp != '.' && _res.options & RES_DNSRCH))
  193.          for (domain = _res.dnsrch; *domain; domain++) {
  194.         ret = res_querydomain(name, *domain, class, type,
  195.             answer, anslen);
  196.         if (ret > 0)
  197.             return (ret);
  198.         /*
  199.          * If no server present, give up.
  200.          * If name isn't found in this domain,
  201.          * keep trying higher domains in the search list
  202.          * (if that's enabled).
  203.          * On a NO_DATA error, keep trying, otherwise
  204.          * a wildcard entry of another type could keep us
  205.          * from finding this entry higher in the domain.
  206.          * If we get some other error (negative answer or
  207.          * server failure), then stop searching up,
  208.          * but try the input name below in case it's fully-qualified.
  209.          */
  210.         if (errno == ECONNREFUSED) {
  211.             h_errno = TRY_AGAIN;
  212.             return (-1);
  213.         }
  214.         if (h_errno == NO_DATA)
  215.             got_nodata++;
  216.         if ((h_errno != HOST_NOT_FOUND && h_errno != NO_DATA) ||
  217.             (_res.options & RES_DNSRCH) == 0)
  218.             break;
  219.     }
  220.     /*
  221.      * If the search/default failed, try the name as fully-qualified,
  222.      * but only if it contained at least one dot (even trailing).
  223.      * This is purely a heuristic; we assume that any reasonable query
  224.      * about a top-level domain (for servers, SOA, etc) will not use
  225.      * res_search.
  226.      */
  227.     if (n && (ret = res_querydomain(name, (char *)NULL, class, type,
  228.         answer, anslen)) > 0)
  229.         return (ret);
  230.     if (got_nodata)
  231.         h_errno = NO_DATA;
  232.     return (-1);
  233. }
  234.  
  235. /*
  236.  * Perform a call on res_query on the concatenation of name and domain,
  237.  * removing a trailing dot from name if domain is NULL.
  238.  */
  239. res_querydomain(name, domain, class, type, answer, anslen)
  240.     char *name, *domain;
  241.     int class, type;    /* class and type of query */
  242.     u_char *answer;        /* buffer to put answer */
  243.     int anslen;        /* size of answer */
  244. {
  245.     char nbuf[2*MAXDNAME+2];
  246.     char *longname = nbuf;
  247.     int n;
  248.  
  249. #ifdef DEBUG
  250.     if (_res.options & RES_DEBUG)
  251.         printf(";; res_querydomain(%s, %s, %d, %d)\n",
  252.             name, domain, class, type);
  253. #endif
  254.     if (domain == NULL) {
  255.         /*
  256.          * Check for trailing '.';
  257.          * copy without '.' if present.
  258.          */
  259.         n = strlen(name) - 1;
  260.         if (name[n] == '.' && n < sizeof(nbuf) - 1) {
  261.             bcopy(name, nbuf, n);
  262.             nbuf[n] = '\0';
  263.         } else
  264.             longname = name;
  265.     } else
  266.         (void)sprintf(nbuf, "%.*s.%.*s",
  267.             MAXDNAME, name, MAXDNAME, domain);
  268.  
  269.     return (res_query(longname, class, type, answer, anslen));
  270. }
  271.  
  272. char *
  273. __hostalias(name)
  274.     register const char *name;
  275. {
  276.     register char *C1, *C2;
  277.     FILE *fp;
  278.     char *file, *getenv(), *strcpy(), *strncpy();
  279.     char buf[BUFSIZ];
  280.     static char abuf[MAXDNAME];
  281.  
  282.     file = getenv("HOSTALIASES");
  283.     if (file == NULL || (fp = fopen(file, "r")) == NULL)
  284.         return (NULL);
  285.     buf[sizeof(buf) - 1] = '\0';
  286.     while (fgets(buf, sizeof(buf), fp)) {
  287.         for (C1 = buf; *C1 && !isspace(*C1); ++C1);
  288.         if (!*C1)
  289.             break;
  290.         *C1 = '\0';
  291.         if (!strcasecmp(buf, name)) {
  292.             while (isspace(*++C1));
  293.             if (!*C1)
  294.                 break;
  295.             for (C2 = C1 + 1; *C2 && !isspace(*C2); ++C2);
  296.             abuf[sizeof(abuf) - 1] = *C2 = '\0';
  297.             (void)strncpy(abuf, C1, sizeof(abuf) - 1);
  298.             fclose(fp);
  299.             return (abuf);
  300.         }
  301.     }
  302.     fclose(fp);
  303.     return (NULL);
  304. }
  305.